#!/usr/bin/env node
"use strict";

require("./util/prototype.js");
const fileUtil = require("./util/fileUtil");

const fs = require("fs");
const xlsx = require("node-xlsx");

const getColIndex = (names, row) => {
    let max = names
        .split("|")
        .map(name => {
            return row
                .map((colName, index) => {
                    if (colName == name) {
                        return 100 + index;
                    } else if (colName.indexOf(name) != -1) {
                        return index;
                    } else {
                        return -1;
                    }
                })
                .max();
        })
        .max();

    if (max >= 100) {
        return max - 100;
    } else {
        return max;
    }
};

const getColIndexs = (names, row) => {
    return names.map(name => {
        return getColIndex(name, row);
    });
};

module.exports = function(
    workFolder,
    apiFolder,
    app,
    ejsName,
    signField,
    signAttr,
    fieldDescColName,
    fieldColName,
    elseCols
) {
    const appPath = `${apiFolder}/${app}/Model/`;
    // 创建目录
    if (!fs.existsSync(`${apiFolder}/${app}/`)) {
        fs.mkdirSync(`${apiFolder}/${app}/`);
    }
    if (!fs.existsSync(appPath)) {
        fs.mkdirSync(appPath);
    }

    fileUtil
        .readDirPromise(
            workFolder,
            file => file.endsWith(".xls") && file.startsWith("_")
        )
        .then(files => {
            files.map(file => {
                var excelFilePath = `${workFolder}${file}`;
                xlsx.parse(excelFilePath)
                    .filter(
                        sheet =>
                            !sheet.name.startsWith("s") &&
                            sheet.name.toLowerCase().indexOf("code") == -1
                    )
                    .map(sheet => {
                        sheet.app = app;

                        sheet.signField = signField;
                        sheet.signAttr = signAttr;
                        sheet.descColIndex = getColIndex(
                            fieldDescColName,
                            sheet.data[0]
                        );
                        sheet.nameColIndex = getColIndex(
                            fieldColName,
                            sheet.data[0]
                        );
                        sheet.cols = getColIndexs(elseCols, sheet.data[0]);

                        let ejsFilePath = process.mainModule.filename.replace('bin\\myCmd.js', `lib/ejs/${ejsName}.ejs`);

                        fileUtil
                            .renderFilePromise(ejsFilePath, sheet)
                            .then(str => {

                                var filePath = `${appPath}${sheet.name}.cs`;

                                fileUtil
                                    .writeFilePromise(str, filePath, "utf-8")
                                    .then(filePath => {
                                        console.log(`${filePath}已保存`);
                                    });
                            })
                            .catch(err => {
                                console.log(err);
                            });
                    });
            });
        });
};
